package com.student.liu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.student.liu.config.RabbitMQConfig;
import com.student.liu.dubbo.IDubboGoodsService;
import com.student.liu.entity.Orderform;
import com.student.liu.exception.ServiceValidationException;
import com.student.liu.mapper.IOrderformMapper;
import com.student.liu.service.IOrderformService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * 订单服务接口 实现类
 */
@Slf4j
@Service
public class OrderformServiceImpl extends ServiceImpl<IOrderformMapper, Orderform> implements IOrderformService {


    @Override
    public Integer createOrderform(Float money, Integer tableId) {
        //当前请求者
        Integer userId = Integer.parseInt(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        //创建订单对象
        Orderform orderform = new Orderform();
        orderform.setStateId((byte) 1);
        orderform.setUserId(userId);
        orderform.setMoney(money);
        orderform.setSubmittime(LocalDateTime.now());
        orderform.setTableId(tableId);

        log.info("构建好的订单对象：" + orderform);
        if (!save(orderform)) {
            throw new ServiceValidationException("订单创建失败，请联系客服。", 402);
        }
//        log.info("订单ID："+orderform.getId());
        return orderform.getId();
    }

    @Override
    public Object slectOrderform(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("orderform.*","orderform_state.name as 'state'");

        queryWrapper.last("JOIN orderform_state ON state_id =orderform_state.id " +
                "WHERE orderform.id = "+id);
        return getOne(queryWrapper);
    }

    @Override
    public IPage<Map<String, Object>> orderPage(Integer pageIndex, Integer pageSize) {
        Integer userId = Integer.parseInt(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        Page page = new Page(pageIndex, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("orderform.*","orderform_state.name as 'state'");
        queryWrapper.last("JOIN orderform_state ON state_id =orderform_state.id " +
                "WHERE user_id = "+userId +
                " order by orderform.id desc");
        return pageMaps(page,queryWrapper);
    }

    @Override
    public void update(Integer id,Byte state) {
        Orderform orderform = new Orderform();
        orderform.setStateId(state);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        update(orderform,queryWrapper);
    }

    @Override
    public void addWaiterId(Integer id) {
        Orderform orderform = new Orderform();
        Integer waiterId = Integer.parseInt(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        orderform.setWaiterId(waiterId);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        update(orderform,queryWrapper);
    }

    @Override
    public void selsctTableId(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        getOne(queryWrapper);
    }


    @Override
    public void remove(Integer id) {
        removeById(id);
    }

}
